package com.example.applications.Kotlin

fun main() {
    val animal = Animal()
    animal.eat()
    animal.tellAge()


    var person = person("小王", 18)
    person.Information()

    var person2 = Person2("小王")
    var person3 = Person2("小王", 18)
    var person4 = Person2("小王", 18, "中国")
//    println(person2)
//    println(person3)
//    println(person4)
}


//普通类的用法
class Animal {
    var name: String = "小猫"
    var age: Int = 3
    var food: String = "鱼"

    fun eat() {
        println(name + "吃" + food)
    }

    fun tellAge() {
        println(name + "今年" + age + "岁")
    }
}


//类的构造函数
class person constructor(var name: String, var age: Int) {
    init {
        println("初始化完成")
        require(age > 0) { "年龄必须大于0" }
        require(name.isNotEmpty()) { "名字不能为空" }
    }

    fun Information() {
        println("姓名：" + name + "年龄：" + age)
    }
}


//多次构造函数
class Person2 constructor(var name: String) {
    var age: Int = 0
    var country: String = ""

    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }

    constructor(name: String, age: Int, country: String) : this(name, age) {
        this.country = country
    }
}